package cn.jiuxiaoer.job.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import cn.jiuxiaoer.job.domain.ShopDetail;
import cn.jiuxiaoer.job.domain.ShopDetailDao;
import cn.jiuxiaoer.job.domain.ShopGoodsDetailDto;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author gzk
 * @date 2021/46/26 19:46
 */
@Log4j2
@Service
public class ShopDetailService {

    @Autowired
    private ShopDetailDao shopDetailDao;

    public Boolean getList() {
        // 查合格、损坏、售后退货未出库的商品
       List<ShopGoodsDetailDto> dtoList = shopDetailDao.getGoodsDetailList();
       log.info("查合格、损坏、售后退货未出库的商品：{}", JSONUtil.toJsonStr(dtoList));
        // 过期商品
        List<ShopDetail> overDue = new ArrayList<>();
        // 临期商品
        List<ShopDetail> interim = new ArrayList<>();
        //当前时间
        Date nowDate = DateUtil.date();
        if (CollUtil.isNotEmpty(dtoList)) {
            dtoList.forEach(dto -> {
                ShopDetail detailDto = new ShopDetail();
                Date expiryDate = dto.getExpiryDate();
                // 如果没有有效期至，就用生产日期+保质期
                if (expiryDate == null) {
                    expiryDate = DateUtil.offsetDay(dto.getProductionDate(), Integer.valueOf(dto.getShelfLife()));
                }
                // 如果当前时间大于保质期至，为过期商品
                if (nowDate.after(expiryDate)) {
                    detailDto.setId(dto.getId());
                    detailDto.setGoodsStatus(2);
                    overDue.add(detailDto);
                } else {
                    //日期相减得到相差的日期
                    long day = (nowDate.getTime()-dto.getProductionDate().getTime())/(24*60*60*1000)>0 ? (nowDate.getTime()-dto.getProductionDate().getTime())/(24*60*60*1000):
                            (dto.getProductionDate().getTime()-nowDate.getTime())/(24*60*60*1000);
                    // 当前日期-生产日期>=临期值，为临期商品
                    if (day >= Long.valueOf(dto.getCriticaValue())) {
                        detailDto.setId(dto.getId());
                        detailDto.setGoodsStatus(1);
                        interim.add(detailDto);
                    }
                }
            });
        }
        // 设置过期商品
        if (CollUtil.isNotEmpty(overDue)) {
            log.info("过期商品: {}", JSONUtil.toJsonStr(overDue));
            Boolean flag = shopDetailDao.updateBatchById(overDue);
            if (flag) {
                log.info("过期商品更新成功");
            } else {
                log.info("过期商品更新失败");
                return false;
            }
        }
        // 设置临期商品
        if (CollUtil.isNotEmpty(interim)) {
            log.info("临期商品: {}", JSONUtil.toJsonStr(interim));
            Boolean flag = shopDetailDao.updateBatchById(interim);
            if (flag) {
                log.info("临期商品更新成功");
            } else {
                log.info("临期商品更新失败");
                return false;
            }
        }
       return true;
    }
}
